package com.harmontronics.hanma.paas.paas.domain.iam.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.UserGroupRoleList;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.valueobject.HmUserGroupRoleFields;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.persistence.HmUserGroupRoleRepository;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.HmUserGroupRole;
import java.util.ArrayList;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.harmontronics.hanma.paas.paas.domain.iam.repository.mapper.HmUserGroupRoleMapper;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.po.HmUserGroupRolePO;
import java.util.List;

@Service("hmUserGroupRoleService")
@DS("#header.App-Unique")
public class HmUserGroupRoleService{

    @Autowired
    private HmUserGroupRoleRepository hmUserGroupRoleRepository;

    @Autowired
    private HmUserGroupRoleMapper hmUserGroupRoleMapper;


    public void remove(List<Long> ids){
            hmUserGroupRoleRepository.removeByIds(ids);
    }


    public void deleteWithRoleIds(List<Long> idList) {
        hmUserGroupRoleMapper.delete(
                new UpdateWrapper<HmUserGroupRolePO>().in(HmUserGroupRoleFields.ROLE_ID, idList));
    }

    /**
     * 查询当前用户所有的用户组权限
     */
    public List<UserGroupRoleList> selectGroupRole(Long groupId) {
        List<UserGroupRoleList> userGroupRoleListList =
                hmUserGroupRoleMapper.selectGroupRoleWithGroupId(groupId);
        return userGroupRoleListList;
    }

    public void create(HmUserGroupRole userGroupRoleCreate) {
        if (userGroupRoleCreate != null) {
            List<Long> roleIds = userGroupRoleCreate.getRoleIds();
            if (CollectionUtils.isNotEmpty(roleIds)) {
                List<HmUserGroupRolePO> userGroupRoles = new ArrayList<>();
                for (Long roleId : roleIds) {
                    userGroupRoles.add(
                            new HmUserGroupRolePO().setRoleId(roleId).setDeleted(Integer.valueOf(0))
                                    .setGroupId(userGroupRoleCreate.getGroupId()));
                }
                if (CollectionUtils.isNotEmpty(userGroupRoles)) {
                    hmUserGroupRoleRepository.saveBatch(userGroupRoles);
                }
            }
        }
    }

}